/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package foalvleditor.gui;

import foalvleditor.core.impl.LevelModel;
import foalvleditor.gameobjects.Bitmap;
import foalvleditor.gameobjects.AbstractGameObject;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

/**
 *
 * @author Mikael
 */
public class JImagePopupMenu extends JPopupMenu {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final AbstractGameObject obj;
    private static LevelModel levelModel=LevelModel.getInstance();

    public JImagePopupMenu(AbstractGameObject goa){
        super();
        this.obj=goa;
        ArrayList<Bitmap> possibleBitmaps=obj.getBitmapFamily();
        try{
            JImageMenuItem newMenuItem;
        for (int i=0; i<possibleBitmaps.size(); i++)
        {
            newMenuItem=new JImageMenuItem(new PopupAction("setImage"));
            newMenuItem.setBitmap(possibleBitmaps.get(i));
            this.add(newMenuItem);
        }

        }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, "Error while processing images.");;
        }
    }

private class PopupAction extends AbstractAction{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final transient String action;
    
    public PopupAction (String evt){
        super();
        this.action=evt;
        putValue(Action.NAME, action);
    }

        public void actionPerformed(ActionEvent e) {
            if (action.contains("setImage"))
            {levelModel.setBitmap(obj,((JImageMenuItem) e.getSource()).getBitmap());
             levelModel.getView().repaintPanel(obj);
             levelModel.getView().repaintAll();
            }

            }
        }
}

    